<?php
/*
	首页
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['ischecklogin']=$this->model('member')->checklogin();
if($_RQ['op']=='main'){
	$this->session('goback','jy_kuaidisys/ji/');
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','avatar','nickname'));
	$member['credit1passtime']=date('Y年m月d日',time()+7*24*3600);
	$return['member']=$member;
	$return['types']=$this->wp_types;
	$return['remark_types']=$this->remark_types;
	$return['kd_types']=$this->kd_types;
	$address=pdo_get('jy_kuaidisys_address',array('isdefault'=>1,'type'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if(!$address){
		$address=pdo_get('jy_kuaidisys_address',array('type'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	}
	$address['pcd']=@json_decode($address['pcd'],true);
	if($address['pcd']['city']=='直辖区'){
		$address['pcd']['city']=$address['pcd']['province'];
	}
	$toaddress=pdo_get('jy_kuaidisys_address',array('isdefault'=>1,'type'=>2,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$toaddress['pcd']=@json_decode($toaddress['pcd'],true);
	if($toaddress['pcd']['city']=='直辖区'){
		$toaddress['pcd']['city']=$toaddress['pcd']['province'];
	}
	$return['post']=array(
		'weight'=>1,
		'typetitle'=>'日用百货',
		'baonum'=>1,
		'endprice'=>0,
		'address'=>$address,
		'toaddress'=>$toaddress,
	);
	$return['kdlist']=pdo_getall('jy_kuaidisys_kdlist',array('status'=>1,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['kdlist'][0]=array('id'=>0,'title'=>'智能下单');
	$return['kdlist']['dewu']=array('id'=>'dewu','title'=>'得物下单');
	$return['kdlist']['zhong']=array('id'=>'zhong','title'=>'重货下单');
	$return['list']=array();
	if($this->plugin_exists('jy_aiaddress')){
		$return['jy_aiaddress']=plugin('jy_aiaddress')->plugin_setting('basic');
	}
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('nocheck'));
	if($member['nocheck']){
		exi('当前会员禁止下单！','error');
	}
	$post=get_postdata();
	$post['money']=$post['endprice'];
	$order_id=$this->model('model')->createlog($post);
	$params=array(
		'id'=>$order_id,
		'title'=>'寄快递',
		'fee'=>$post['money'],
		'oldfee'=>$post['oldfee'],
		'back_url'=>'jy_kuaidisys/ji/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
		'success_url'=>'jy_kuaidisys/ji_log/',
		'error_url'=>'jy_kuaidisys/ji_log/',
	);
	$payid=cfc('pay')->create_pay_order($params);
	$this->pdo_update('jilog',array('pay_id'=>$payid),array('id'=>$order_id));
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
		'success_url'=>$success_url,
		'error_url'=>$error_url,
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_kuaidisys/ji_log/');
	}
	$order=pdo_get('jy_kuaidisys_jilog',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_kuaidisys/ji_log/');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['money'].'|'.$payresult['fee'],'error','jy_kuaidisys/ji_log/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	$this->model('model')->jilog_pay($order['id'],$params);
	exi('提交成功!');
}
// 获取渠道
if($_RQ['op']=='getfeekd'){
	$post=get_postdata();
	$result=$this->model('model')->getfeekd($post);
	$list=array();
	foreach($result as $v){
		if(!$list[$v['tagType']]){
			$list[$v['tagType']]=$v;
		}
	}
	$res['list']=$list;
	exi($res);
}
// 计算运费
if($_RQ['op']=='getfee'){
	$post=get_postdata();
	$fee=$this->model('model')->getfee($post);
	$res['endprice']=$fee;
	exi($res);
}
?>